package com.blog.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

/**
 * @className: LoginInterceptor
 * @description:用户拦截器
 * @author: HCl
 * @createTime: 2020年5月27日 上午7:12:28
 */
public class LoginInterceptor implements HandlerInterceptor{

	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		// TODO Auto-generated method stub
		HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
	}

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		// TODO Auto-generated method stub
		HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
	}

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
//		// 获取请求的URL
//        String url = request.getRequestURI();
//        // login.jsp或登录请求放行，不拦截
//        if (url.indexOf("/toLogin") >= 0 || url.indexOf("/login") >= 0) {
//            return true;
//        }
//        // 获取 session
//        HttpSession session = request.getSession();
//        Object obj = session.getAttribute("user");
//        if (obj != null)
//            return true;
//        // 没有登录且不是登录页面，转发到登录页面，并给出提示错误信息
//        request.setAttribute("msg", "还没登录，请先登录！");
//        request.getRequestDispatcher("/WEB-INF/jsp/login.jsp").forward(request,
//                response);
        return true;
		
	}

	
}
